package com.android1.amarena2d.test;

import com.android1.amarena2d.actions.base.Action;
import com.android1.amarena2d.actions.base.FunctionAction;
import com.android1.amarena2d.actions.base.IntervalAction;
import com.android1.amarena2d.actions.base.Sequence;
import com.android1.amarena2d.actions.ease.*;
import com.android1.amarena2d.actions.instant.Place;
import com.android1.amarena2d.actions.movement.MoveTo;
import com.android1.amarena2d.commons.VoidFunction;
import com.android1.amarena2d.nodes.EntityGroup;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.nodes.SimpleLabel;
import com.android1.amarena2d.nodes.sprites.SharedSprite;
import com.android1.amarena2d.texture.BitmapFont;
import com.badlogic.gdx.graphics.Color;

public class EaseActionExamplesScene extends Scene {

    boolean waiting;

    final EntityGroup  box;
    final SimpleLabel text;

    final IntervalAction movein = MoveTo.$(240, 150, 2);
    final IntervalAction moveout = MoveTo.$(480, 150, 2);
    final Place placeToStart = Place.$(0, 150);

    final VoidFunction onWait = new VoidFunction() {

        public void exec() {
            waiting = true;
            box.action().pauseAll();
        }
    };

    final VoidFunction onFinished = new VoidFunction() {
        @Override
        public void exec() {
            Action nextAction = findNext();
            box.action().run(nextAction);
        }
    };

    Action easeBouceOut$easeBounceIn = Sequence.$(placeToStart, EaseBounceOut.$(movein), FunctionAction.$(onWait), EaseBounceIn.$(moveout), FunctionAction.$(onFinished));
    Action easeBounceInOut = Sequence.$(placeToStart, EaseBounceInOut.$( movein), FunctionAction.$(onWait), EaseBounceInOut.$( moveout), FunctionAction.$(onFinished));
    Action easeOut$easeIn = Sequence.$(placeToStart, EaseIn.$(0.2F, movein), FunctionAction.$(onWait),EaseOut.$(0.2F, moveout), FunctionAction.$(onFinished));
    Action easeBackOut$easeBackIn = Sequence.$(placeToStart, EaseBackOut.$( movein), FunctionAction.$(onWait), EaseBackIn.$( moveout), FunctionAction.$(onFinished));
    Action easeBackInOut = Sequence.$(placeToStart, EaseBackInOut.$( movein), FunctionAction.$(onWait), EaseBackInOut.$( moveout), FunctionAction.$(onFinished));

    Action[] easeActions = new Action[]{
            easeBouceOut$easeBounceIn,
            easeBounceInOut,
            easeOut$easeIn,
            easeBackOut$easeBackIn ,
            easeBackInOut
    };

    String[] actionNames = new String[]{
            "EaseBounceOut / EaseBounceIn",
            "EaseBounceInOut / EaseBounceInOut",
            "EaseOut / EaseIn" ,
            "EaseBackOut / EaseBackIn" ,
            "EaseBackInOut / EaseBackInOut" ,
    };

    int actionIndex = 0;


    public EaseActionExamplesScene() {

        //engine.getRenderer().setClearColor(new Color(227F / 255F, 216F / 255F, 196F / 255F, 1));
        engine.getRenderer().setClearColor(Color.WHITE);

        touch().makeTouchable(this);

        box = new EntityGroup(240,150);
        final SharedSprite back = new SharedSprite("assets/box.png");
        box.add(back);

        text = new SimpleLabel(0, 0, Color.BLACK).setAlign(BitmapFont.HAlignment.CENTER);
        box.add(text);

        add(box);

        add(new SimpleLabel("(Tab box for next EaseAction)", 240, 20, Color.BLACK).setAlign(BitmapFont.HAlignment.CENTER));

    }


    @Override
    protected void onEnterFinished() {
        text.setText(actionNames[0]);
        box.action().run(easeActions[0]);
    }


    private Action findNext() {
        actionIndex++;
        if (actionIndex >= easeActions.length) {
            actionIndex = 0;
        }
        text.setText(actionNames[actionIndex]);
        return easeActions[actionIndex];
    }


    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
        if (waiting) {
            box.action().resumeAll();
            waiting = false;
        }
        return true;
    }
}